#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <typeinfo>
#include<string>
#include "thread"

#include "ArmorDector.h"
#include "general.h"
#include "ArmorDector.h"
#include "opencv_extend.h"
#include "log.h"
#include "Phoenix.h"
#include "img_buffer.h"
#include"general.h"
#include"Timestamp.h"
using namespace std;
using namespace cv;
Phoenix::Phoenix()
{
    sem_init(&sem_pro , 0 , 0 );
    sem_init(&sem_com , 0 , 1 );
    _workState = make_shared<Work>();
    bool err = serial.InitPort();
    if(err == Serial::USB_CANNOT_FIND)
    {
        LOG_ERROR << "USB_CANNOT_FIND";
    }
}
Phoenix::~Phoenix()
{
    sem_destroy(&sem_pro);
    sem_destroy(&sem_com);
}
void Phoenix::Produce()
{
    cv::Mat src;
	//打开
	while (!mycamera.open())
		;
	//设置相机参数
	while (!mycamera.setVideoparam())
		;
	//不断读取图片
	while (!mycamera.startStream());
	while (1)
	{
		if (!mycamera.getVideoimage())
		{
			continue;
		}
		if (!mycamera.rgbtocv())
		{
			continue;
		}
        src = mycamera.getiamge();
        if(src.empty())
        {
            LOG_ERROR << "src empty";
            continue;
        }
        sem_wait(&sem_com);
        try
        {
            _buffer.ImgEnterBuffer(src);
        }
        catch (...)
        {
            std::cout << "照片读如出错" << std::endl;
            throw;
        }
        sem_post(&sem_pro);
    }
	while (!mycamera.closeStream());
}
thread Phoenix::ConsumeThread()
{
    return thread(&Phoenix::Consume ,this);
}
thread Phoenix::ProduceThread()
{
    return thread(&Phoenix::Produce ,this);
}
thread Phoenix::SenseThread()
{
    return thread(&Phoenix::Sense ,this);
}

